package com.ruoyi.user.service.impl;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.user.dependency.CmsService;
import com.ruoyi.user.domain.UserOrder;
import com.ruoyi.user.mapper.UserOrderMapper;
import com.ruoyi.user.model.CmsGoods;
import com.ruoyi.user.service.IUserOrderService;
import com.ruoyi.user.utils.PayChannel;
import com.ruoyi.user.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户订单Service业务层处理
 *
 * @author houyong
 * @date 2025-11-20
 */
@Service
public class UserOrderServiceImpl implements IUserOrderService {
    @Autowired
    private UserOrderMapper userOrderMapper;

    @Autowired
    private CmsService cmsService;

    /**
     * 查询用户订单
     *
     * @param id 用户订单主键
     * @return 用户订单
     */
    @Override
    public UserOrder selectUserOrderById(Long id) {
        return userOrderMapper.selectUserOrderById(id);
    }

    /**
     * 查询用户订单列表
     *
     * @param userOrder 用户订单
     * @return 用户订单
     */
    @Override
    public List<UserOrder> selectUserOrderList(UserOrder userOrder) {
        return userOrderMapper.selectUserOrderList(userOrder);
    }


    /**
     * @param goodsId 用户订单
     * @param uid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertUserOrder(Integer goodsId, String uid) {
        CmsGoods goods = cmsService.getGoodsInfo(goodsId.longValue());
        if (goods == null) {
            return AjaxResult.error("商品不存在");
        }
        UserOrder userOrder = new UserOrder();
        userOrder.setUid(uid);
        userOrder.setGoodsId(goodsId);
        userOrder.setGoodsSubject(goods.getName());
        userOrder.setBody(goods.getName());
        userOrder.setTotalFee(goods.getPlatformPrice());
        userOrder.setNeedPayTotalFee(goods.getPlatformPrice());
        userOrder.setNeedPayFee(goods.getPlatformPrice().multiply(new BigDecimal(100)).intValue());
        userOrder.setFee(goods.getPlatformPrice().multiply(new BigDecimal(100)).intValue());
        userOrder.setT(System.currentTimeMillis());
        userOrder.setStatus(PayChannel.TransactionStatus.NOTPAY.getValue());
        userOrder.setPayTimeout(System.currentTimeMillis() + 1000 * 60 * 60);
        String orderId = "ID" + System.currentTimeMillis() + RandomUtil.getRandomNumber();
        userOrder.setOrderId(orderId);
        int i = userOrderMapper.insertUserOrder(userOrder);


        if (i <= 0) {
            return AjaxResult.error("新增用户订单失败");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("orderId", userOrder.getOrderId());
        return AjaxResult.success(data);
    }

    /**
     * 修改用户订单
     *
     * @param userOrder 用户订单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUserOrder(UserOrder userOrder) {
        return userOrderMapper.updateUserOrder(userOrder);
    }

    /**
     * 批量删除用户订单
     *
     * @param ids 需要删除的用户订单主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUserOrderByIds(Long[] ids) {
        return userOrderMapper.deleteUserOrderByIds(ids);
    }

    /**
     * 删除用户订单信息
     *
     * @param id 用户订单主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUserOrderById(Long id) {
        return userOrderMapper.deleteUserOrderById(id);
    }

    @Override
    public UserOrder findOrderByOrderId(String orderId) {
        return userOrderMapper.findOrderByOrderId(orderId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void thirdPaySuccess(UserOrder order, int totalFeeI, Long payTimeL) {
        order.setPayTime(payTimeL);
        order.setNeedPayFee(totalFeeI);
        userOrderMapper.updateUserOrder(order);
    }
}
